﻿using Architecture.Repository.Interface.Traceability;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Domain.DeepProcessingDomain;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.DeepProcessingServices
{
    /// <summary>
    /// 深加工服务
    /// </summary>
    public class DeepProcessingService : IDeepProcessingService
    {
        /// <summary>
        /// 深加工仓储
        /// </summary>
        protected readonly IDeepProcessingRepository deepProcessingRepository;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deepProcessingRepository">深加工仓储</param>
        /// <param name="pictureRepository">图片仓储</param>
        public DeepProcessingService(IDeepProcessingRepository deepProcessingRepository, IPictureRepository pictureRepository)
        {
            this.deepProcessingRepository = deepProcessingRepository;
            this.pictureRepository = pictureRepository;
        }

        /// <summary>
        /// 添加深加工信息
        /// </summary>
        /// <param name="deepProcessing">深加工实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> AddDeepProcessing(DeepProcessing deepProcessing)
        {
            return await deepProcessingRepository.AddAsync(deepProcessing);
        }

        /// <summary>
        /// 删除深加工信息
        /// </summary>
        /// <param name="deepProcessingId">深加工ID</param>
        /// <param name="userMame">用户名称</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> DeleteDeepProcessing(int deepProcessingId, string userMame)
        {
            var res = await deepProcessingRepository.GetModelAsync(deepProcessingId);
            res.IsDeleted = true;
            res.DeletedBy = userMame;
            res.DeletedDate = DateTime.Now;
            return await deepProcessingRepository.UpdateAsync(res);
        }

        /// <summary>
        /// 查询深加工信息
        /// </summary>
        /// <param name="shoppingId">商品ID</param>
        /// <returns>返回深加工信息列表</returns>
        public async Task<List<DeepProcessingDto>> GetAllDeepProcessingList(int shoppingId)
        {
            var list = (from a in await deepProcessingRepository.GetAllAsync()
                       join b in await pictureRepository.GetAllAsync()
                       on a.DeepProcessingId equals b.DeepProcessingId
                       where a.ShoppingId == shoppingId && !a.IsDeleted && !b.IsDeleted
                       select new DeepProcessingDto
                       {
                           DeepProcessingId = a.DeepProcessingId,
                           TeamsAndGroups = a.TeamsAndGroups,
                           PersonInCharge = a.PersonInCharge,
                           ProductionBatch = a.ProductionBatch,
                           ShoppingId = shoppingId,
                           CreatedBy = a.CreatedBy,
                           CreatedDate = a.CreatedDate,
                           UpdatedBy = a.UpdatedBy,
                           UpdatedDate = a.UpdatedDate,
                           DeletedBy = a.DeletedBy,
                           DeletedDate = a.DeletedDate,
                           IsDeleted = a.IsDeleted,
                           PictureId = b.PictureId,
                           PictureName = b.PictureName,
                       }).ToList();
            return list;
        }

        /// <summary>
        /// 反填深加工信息
        /// </summary>
        /// <param name="deepProcessingId">深加工ID</param>
        /// <returns>反填深加工信息</returns>
        public async Task<DeepProcessing> GetDeepProcessingModel(int deepProcessingId)
        {
            return await deepProcessingRepository.GetModelAsync(deepProcessingId);
        }

        /// <summary>
        /// 修改深加工
        /// </summary>
        /// <param name="deepProcessing">深加工实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> UpdateDeepProcessing(DeepProcessing deepProcessing)
        {
            var res = await deepProcessingRepository.GetAsync(x => x.DeepProcessingId == deepProcessing.DeepProcessingId);
            res.FirstOrDefault().DeepProcessingId = deepProcessing.DeepProcessingId;
            res.FirstOrDefault().TeamsAndGroups=deepProcessing.TeamsAndGroups;
            res.FirstOrDefault().PersonInCharge=deepProcessing.PersonInCharge;
            res.FirstOrDefault().UpdatedBy=deepProcessing.UpdatedBy;
            res.FirstOrDefault().UpdatedDate=DateTime.Now;
            return await deepProcessingRepository.UpdateAsync(res.FirstOrDefault());
        }
    }
}
